package com.bw.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.tencent.mmkv.MMKV

/**
 * 路由参数拦截器
 */
@Interceptor(priority = 5, name = "路由参数拦截器")
class RouterParamsInterceptor:IInterceptor {
    private lateinit var mContext:Context
    private val needInterceptorPaths = arrayOf("/activity/main","/activity/detail")
    override fun init(context: Context?) {
        if(context!=null){
            mContext=context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        try {
            var routhPath = postcard.path
            var sessionId = MMKV.defaultMMKV().decodeString("sessionId","123232")
            //获取路由参数
            var routeParams = postcard.extras
            if(needInterceptorPaths.contains(routhPath) && !routeParams.containsKey("sessionId")){
                postcard.withString("sessionId",sessionId)
            }
            //继续执行跳转逻辑
            callback.onContinue(postcard)
        }catch (e:Exception){
            e.printStackTrace()
            callback.onContinue(postcard)
        }

    }

}